/*
 * SeasonCalculator.java
 *
 * Created on 20 f�vrier 2008, 13:48
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package br.great.functionalCore;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author defautce
 */
public class SeasonCalculator {
    
    /** Creates a new instance of SeasonCalculator */
    public static String calculateSeason(Date date, double lat){
        //Calculates Season
        String seasons[]={"Winter","Spring","Summer","Autumn"};
        int i=0;
        try{            
            Date springNorthBegin= SeasonCalculator.parseTimestamp("20/03/2008"); 
            Date summerNorthBegin= SeasonCalculator.parseTimestamp("21/06/2008");
            Date autumnNorthBegin= SeasonCalculator.parseTimestamp("23/09/2008");                 
            Date winterNorthBegin= SeasonCalculator.parseTimestamp("21/12/2008");                 
            Calendar cal=Calendar.getInstance();
            cal.setTime(date);
            cal.set(Calendar.YEAR,2008);
            date=cal.getTime();
            
            if(date.before(springNorthBegin)){
                i=0; //winter in the north or summer in the south
            }
            else if(date.before(summerNorthBegin)){
                i=1; //spring in the north or autumn in the south
            
            }
            else if(date.before(autumnNorthBegin)){
                i=2; //summer in the north or winter in the south
            }
            else if(date.before(winterNorthBegin)){
                i=3; //autumn in the or spring in the south
            }
            
            if(lat<0){// The photo was taken in the south 
                i=(i+2)%4; //Invert the season
            }
            
        }catch(Exception e){
            e.printStackTrace();
        }
        return seasons[i];
    }
    
   public static Date parseTimestamp(String timestamp) throws Exception {
       /*
       ** we specify Locale.US since months are in english
       */
       SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
       Date d = sdf.parse(timestamp);   
       return d; 
   } 
    
}
